﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROS.Application.DomainObjects;
using ROS.Persistence.DAO;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence
{
    internal class MenuItemDataMapper : IDataMapper<MenuItem>
    {
        #region Fields

        private IMenuItemDAO _menuItemDAO;
        private IMenuItemCategoryDAO _menuItemCategoryDAO;
        private IMenuItemPricingDAO _menuItemPricingDAO;

        #endregion

        #region Properties

        /// <summary>
        /// Get the DAO to use in this instance of the mapper.
        /// </summary>
        private IMenuItemDAO menuItemDAO
        {
            get
            {
                if (_menuItemDAO == null)
                {
                    DAOFactory daoFactory = DAOFactory.GetDAOFactory("MySQL");
                    _menuItemDAO = daoFactory.GetMenuItemDAO();
                }
                return _menuItemDAO;
            }
        }

        /// <summary>
        /// Get the DAO to use in this instance of the mapper.
        /// </summary>
        private IMenuItemCategoryDAO menuItemCategoryDAO
        {
            get
            {
                if (_menuItemCategoryDAO == null)
                {
                    DAOFactory daoFactory = DAOFactory.GetDAOFactory("MySQL");
                    _menuItemCategoryDAO = daoFactory.GetMenuCategoryItemDAO();
                }
                return _menuItemCategoryDAO;
            }
        }

        /// <summary>
        /// Get the DAO to use in this instance of the mapper.
        /// </summary>
        private IMenuItemPricingDAO menuItemPricingDAO
        {
            get
            {
                if (_menuItemPricingDAO == null)
                {
                    DAOFactory daoFactory = DAOFactory.GetDAOFactory("MySQL");
                    _menuItemPricingDAO = daoFactory.GetMenuItemPricingDAO();
                }
                return _menuItemPricingDAO;
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Construct a new MenuItemDataMapper object.
        /// </summary>
        public MenuItemDataMapper()        
        {
            _menuItemDAO = null;
            _menuItemCategoryDAO = null;
            _menuItemPricingDAO = null;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Insert a menu item into the data source.
        /// </summary>
        /// <param name="menuItem"></param>
        /// <returns></returns>
        public void Create(MenuItem menuItem)
        {
            MenuItemPricingDTO menuItemPricingDTO = new MenuItemPricingDTO();            
            menuItemPricingDTO.Amount = menuItem.Price.Amount;
            menuItemPricingDTO.CurrencyCode = menuItem.Price.Currency.Code;
            menuItemPricingDAO.Insert(menuItemPricingDTO);
            menuItem.Price.ID = menuItemPricingDTO.ID;

            MenuItemDTO menuItemDTO = LoadMenuItemDTO(menuItem);
            menuItemDAO.Insert(menuItemDTO);
            menuItem.ID = menuItemDTO.ID;

            foreach (MenuCategory menuCategory in menuItem.MenuCategories)
            {
                MenuItemCategoryDTO menuItemCategoryDTO = new MenuItemCategoryDTO();
                menuItemCategoryDTO.MenuItemID = menuItemDTO.ID;
                menuItemCategoryDTO.MenuCategoryID = menuCategory.ID;
                menuItemCategoryDAO.Insert(menuItemCategoryDTO);
            }            
        }

        /// <summary>
        /// Find a single menu item by its menuItemID.
        /// </summary>
        /// <param name="menuItemID"></param>
        /// <returns></returns>
        public MenuItem FindByID(long menuItemID)
        {
            MenuItemDTO menuItemDTO = menuItemDAO.SelectByID(menuItemID);
            return LoadMenuItem(menuItemDTO);
        }

        /// <summary>
        /// Find all of the menu items in the system.
        /// </summary>
        /// <returns></returns>
        public IList<MenuItem> FindAll()
        {
            IList<MenuItemDTO> menuItemDTOs = menuItemDAO.SelectAll();
            List<MenuItem> menuItems = new List<MenuItem>();
            foreach (MenuItemDTO menuItemDTO in menuItemDTOs)
            {
                menuItems.Add(LoadMenuItem(menuItemDTO));
            }
            return menuItems;
        }

        /// <summary>
        /// Find all Menu Items matching a particular category.
        /// </summary>
        /// <param name="menuItem"></param>
        /// <returns></returns>
        public IList<MenuItem> FindAllByCategory(MenuCategory menuCategory)
        {
            IList<MenuItemCategoryDTO> menuItemCategoryDTOs =
                menuItemCategoryDAO.SelectByCategoryID(menuCategory.ID);
            
            List<MenuItem> menuItems = new List<MenuItem>();
            foreach (MenuItemCategoryDTO menuItemCategoryDTO in menuItemCategoryDTOs)
            {
                MenuItemDTO menuItemDTO = menuItemDAO.SelectByID(menuItemCategoryDTO.MenuItemID);
                menuItems.Add(LoadMenuItem(menuItemDTO));
            }

            return menuItems;
        }

        /// <summary>
        /// Update a MenuItem
        /// </summary>
        /// <param name="menuItem"></param>
        public void Update(MenuItem menuItem)
        {
            MenuItemDTO menuItemDTO = LoadMenuItemDTO(menuItem);
            menuItemDAO.Update(menuItemDTO);
        }

        /// <summary>
        /// Delete a MenuItem
        /// </summary>
        /// <param name="menuItem"></param>
        public void Delete(MenuItem menuItem)
        {
            menuItemDAO.DeleteByID(menuItem.ID);
        }

        /// <summary>
        /// Load a menu item from a MenuItemDTO.
        /// </summary>
        /// <param name="menuItemDTO"></param>
        /// <returns></returns>
        private MenuItem LoadMenuItem(MenuItemDTO menuItemDTO)
        {
            MenuItemPricingDTO menuItemPricing = menuItemPricingDAO.SelectByID(menuItemDTO.ID);
            Money price = new Money(menuItemPricing.ID, menuItemPricing.Amount, 
                Currency.GetCurrencyByCode(menuItemPricing.CurrencyCode));
            return new MenuItem(menuItemDTO.ID, menuItemDTO.IsActive,
                                menuItemDTO.Name, menuItemDTO.IsBackendFulfilled,
                                price);
        }

        /// <summary>
        /// Load a menu item DTO from a menu item.
        /// </summary>
        /// <param name="menuItem"></param>
        /// <returns></returns>
        private MenuItemDTO LoadMenuItemDTO(MenuItem menuItem)
        {
            MenuItemDTO menuItemDTO = new MenuItemDTO();
            menuItemDTO.Name = menuItem.Name;
            menuItemDTO.IsActive = menuItem.IsActive;
            menuItemDTO.IsBackendFulfilled = menuItem.IsBackendFulfilled;
            menuItemDTO.ID = menuItem.Price.ID;
            menuItemDTO.MenuItemPricingID = menuItem.Price.ID;
            return menuItemDTO;
        }

        #endregion
    }
}
